import axios from 'axios'
const objDeepCopy = function (source) {
  let sourceCopy = source instanceof Array ? [] : {};
  for (let item in source) {
    sourceCopy[item] = typeof source[item] === 'object' ? objDeepCopy(source[item]) : source[item];
  }
  return sourceCopy;
}
//获取日期字符串 YYYY-MM-DD hh:mm:ss
const getDateString = function (val) {
  let d = val;
  let year = d.getFullYear();
  let month = changeTime(d.getMonth() + 1);
  let day = changeTime(d.getDate());
  let hour = changeTime(d.getHours());
  let minute = changeTime(d.getMinutes());
  let second = changeTime(d.getSeconds());

  function changeTime(t) {
    if (t < 10) {
      return "0" + t;
    } else {
      return t;
    }
  };
  let time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  return time;
};

//打印div内容，printID为div容器ID
const printdiv = function (printID) {
  let style;
  axios({
    method: 'get',
    url: '../../../static/css/print.css'
  }).then((res) => {
    style = `<style>${res.data}</style>`;
  });

  setTimeout(function () {
    let headhtml = `<html><head><title></title>${style}</head><body>`;
    let foothtml = "</body>";
    // 获取div中的html内容
    let newhtml = document.all.item(printID).innerHTML;

    // 获取原来的窗口界面body的html内容，并保存起来
    let oldhtml = document.body.innerHTML;

    //打开新窗口
    let newWindow = window.open("打印窗口", "_blank");

    // 给窗口界面重新赋值，赋自己拼接起来的html内容
    newWindow.document.body.innerHTML = headhtml + newhtml + foothtml;

    // 调用window.print方法打印新窗口
    newWindow.print();
    // 将原来窗口body的html值回填展示
    // newWindow.document.body.innerHTML = oldhtml;
    //关闭窗口
    newWindow.close()
    return false;
  }, 600);
};
/* cookie操作系列 */
const DRcookie = {
  set(key, val, time) { //设置cookie方法
    var date = new Date(); //获取当前时间
    var expiresDays = time; //将date设置为n天以后的时间
    date.setTime(date.getTime() + expiresDays * 1 * 3600 * 1000); //格式化为cookie识别的时间
    document.cookie = key + "=" + val + ";expires=" + date.toGMTString(); //设置cookie
  },
  get(key) { //获取cookie方法
    /*获取cookie参数*/
    var getCookie = document.cookie.replace(/[ ]/g, ""); //获取cookie，并且将获得的cookie格式化，去掉空格字符
    var arrCookie = getCookie.split(";") //将获得的cookie以"分号"为标识 将cookie保存到arrCookie的数组中
    var tips; //声明变量tips
    for (var i = 0; i < arrCookie.length; i++) { //使用for循环查找cookie中的tips变量
      var arr = arrCookie[i].split("="); //将单条cookie用"等号"为标识，将单条cookie保存为arr数组
      if (key == arr[0]) { //匹配变量名称，其中arr[0]是指的cookie名称，如果该条变量为tips则执行判断语句中的赋值操作
        tips = arr[1]; //将cookie的值赋给变量tips
        break; //终止for循环遍历
      }
    };
    return tips;
  },
  delete(key) { //删除cookie方法
    var date = new Date(); //获取当前时间
    date.setTime(date.getTime() - 10000); //将date设置为过去的时间
    document.cookie = key + "=v; expires =" + date.toGMTString(); //设置cookie
  }
}

/* cookie操作系列 */

export {
  objDeepCopy,
  getDateString,
  printdiv,
  DRcookie
}
